/*
 * Copyright 2018-10-1 20:47:06 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.erp.ao.PaymentAO;
import com.lap.scp.erp.core.PaymentManager;
import com.lap.scp.erp.domain.PaymentDO;
import com.lap.scp.erp.query.PaymentQueryVO;

/**
 * <pre>
 *   默认 其他成本应用实现类，提供其他成本相关的流程应用操作、查询等。
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-1 20:47:06
 * @since 2.0.0
 */
@Service("paymentAO")
public class DefaultPaymentAO extends AbstractAO implements PaymentAO {
	/**
	 * 其他成本业务操作对象
	 */
	@Resource
	private PaymentManager paymentManager;

	@Override
	public Resp<PaymentDO> insertPayment(PaymentDO paymentDO) {
		Resp<PaymentDO> resp = new Resp<>();
		try {
			this.validate(resp, paymentDO);
			if (resp.isSuccess()) {
				paymentManager.insertPayment(paymentDO);
				resp.setData(paymentDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(paymentDO, e);
		}
		return resp;
	}

	/**
	 * 简单校验数据非法性
	 */
	private void validate(Resp<?> resp, PaymentDO paymentDO) {
		if (paymentDO == null) {
			resp.putMsg(getMsg("200"));
		} else {
			if (paymentDO.getContractId() == null) {
				resp.putMsg(getMsg("201"));
			}
			if (StringUtils.isBlank(paymentDO.getName())) {
				resp.putMsg(getMsg("203"));
			}
			if (paymentDO.getBuyDate() == null) {
				resp.putMsg(getMsg("204"));
			}
			if (paymentDO.getAmount() == null) {
				resp.putMsg(getMsg("205"));
			}
		}
	}

	@Override
	public Resp<Integer> deleteById(Integer paymentId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = paymentManager.deleteById(paymentId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(paymentId, e);
		}
		return resp;
	}

	@Override
	public Resp<PaymentDO> queryById(Integer paymentId) {
		Resp<PaymentDO> model = new Resp<>();
		try {
			PaymentDO paymentDO = paymentManager.queryById(paymentId);
			model.setData(paymentDO);
		} catch (Exception e) {
			model.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(paymentId, e);
		}
		return model;
	}

	@Override
	public Resp<List<PaymentDO>> queryDetail(PaymentQueryVO queryVO) {
		Resp<List<PaymentDO>> resp = new Resp<>();
		try {
			List<PaymentDO> list = paymentManager.queryDetail(queryVO);
			resp.setData(list);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(new Object[] { queryVO }, e);
		}
		return resp;
	}

	@Override
	public Resp<Integer> updatePayment(PaymentDO paymentDO) {
		Resp<Integer> resp = new Resp<>();
		try {
			this.validate(resp, paymentDO);
			if (resp.isSuccess()) {
				int flag = paymentManager.updatePayment(paymentDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(paymentDO, e);
		}
		return resp;
	}

}
